/**
 * @Author YuanYuan
 * @Date 2022/7/11
 * @Time 15:52
 */
// 1、无头单向非循环链表实现
public class SingleLinkedList {
    static class Node {

        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    public Node head;
    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = head;
        head = node;
        
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        Node cur = head;
        if(cur == null) {
            head = node;
        }
        while(cur!=null) {
            //找到尾
            if(cur.next == null) {
                   cur.next = node;
                   break;
            }
            cur = cur.next;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index == 0) {
            addFirst(data);
        }
        if(index == size()) {
            addLast(data);
        }
        Node node = new Node(data);
        Node cur = head;
        int value = 0;
        while (cur!=null) {
            if(value+1 == index) {
                node.next = cur.next;
                cur.next = node;
                return true;
            }
            cur = cur.next;
            value++;
        }
        return false;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node cur = head;
        //只有一个结点
        if(head.value == key) {
            if(head.next == null) {
                head = null;
                return;
            }else {
                head = head.next;
                return;
            }
        }
        while(cur != null) {
            if(cur.next!=null) {
                if(cur.next.value == key) {
                    cur.next = cur.next.next;
                    return;
                }
            }
            cur = cur.next;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return ;
        }
        Node cur = head;

        while(cur!=null) {
            //第一个结点是val
            if(cur.value == key) {
                //只有一个结点
                if(cur.next == null) {
                    head = null;
                    return ;
                }
                cur = cur.next;
                head = cur;
            }else if(cur.next.value == key) {
                cur.next = cur.next.next;
            }
            cur = cur.next;

        }

    }
    //得到单链表的长度
    public int size() {
        Node cur = head;
        int value = 0;
        while(cur != null) {
             cur = cur.next;
             value++;
        }
        return value;
    }
    public void display() {
        Node cur = head;
        while(cur != null) {
            System.out.println(cur.value);
            cur = cur.next;
        }
    }
    public void clear() {
        head = null;
    }
}
